package com.nine.system.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.nine.common.entity.UserInfo;
import com.nine.common.tool.ResponseResult;
import com.nine.system.mapper.UserInfoMapper;
import com.nine.system.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public ResponseResult changePassword(String originPwd, String afterPwd) {
        long uid = StpUtil.getLoginIdAsLong();

        UserInfo userInfo = userInfoMapper.selectById(uid);
        String originPwdAfter = DigestUtils.md5DigestAsHex(originPwd.getBytes());

        if(!userInfo.getPassword().equals(originPwdAfter)){
            return ResponseResult.fail("原密码输入错误");
        }

        userInfo.setPassword(DigestUtils.md5DigestAsHex(afterPwd.getBytes()));
        userInfoMapper.updateById(userInfo);
        return ResponseResult.ok("更改密码成功");
    }
}
